package com.kinghunt.misp.web.controller.custx;

import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.custx.CustDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.common.param.custx.CustParam;
import com.kinghunt.misp.service.api.custx.CustService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;
/**
 * 客户管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@RestController
@RequestMapping("/api/cust/")
public class CustController {

    @Autowired(required = false)
    private CustService custService;

    /**
     * 列表显示：显示客户列表
     */
    @GetMapping("getCustList")
    public ApiResult<PageInfo<CustDTO>> getCustList(CustParam custParam) {
        return custService.getCustList(custParam);
    }
    /**
     * 客户详情展示
     */
    @GetMapping("getCustDetail")
    public ApiResult<CustDTO> getCustDetail(Long custId) {
        if(Objects.isNull(custId)){
            return ApiResult.failOfMessage("查询失败，请输入客户信息！");
        }
        return custService.getCustDetail(custId);
    }
    /**
     * 新增客户
     */
    @PostMapping(value = "addCust", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult addCust(@Valid @RequestBody CustParam custParam) {
        if(Objects.isNull(custParam.getCustNo())){
            return ApiResult.failOfMessage("新增失败，请输入客户编号！");
        }
        UserDTO loginUser = getCurrentUser();
        custParam.setCreatorId(loginUser.getId());
        custParam.setUpdaterId(loginUser.getId());
        return custService.addCust(custParam);
    }
    /**
     * 修改客户
     */
    @PostMapping(value = "updateCust", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult updateCust(@Valid @RequestBody CustParam custParam) {
        UserDTO loginUser = getCurrentUser();
        custParam.setUpdaterId(loginUser.getId());
        return custService.updateCust(custParam);
    }
    /**
     * 删除客户
     *
     * @param custId 角色ID
     * @return 是否成功
     */
    @DeleteMapping("deleteCust")
    public ApiResult<Boolean> deleteCust(Long custId) {
        if(Objects.isNull(custId)){
            return ApiResult.failOfMessage("删除失败，请输入客户信息！");
        }
        return custService.deleteCust(custId);
    }

}
